﻿using Dapper;
using DapperEx;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string connectionName = "strSql";
        public DbBase CreateDbBase()
        {
            return new DbBase(connectionName);
        }
     
        public void Connection()
        {
            var providerName = "System.Data.SqlClient";
            var connStr = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
            if (!string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[connectionName].ProviderName))
                providerName = ConfigurationManager.ConnectionStrings[connectionName].ProviderName;

            var dbProvider = DbProviderFactories.GetFactory(providerName);
            var conn = dbProvider.CreateConnection();
            conn.ConnectionString = connStr;
            conn.Open();
            Console.WriteLine("状态：" + conn.State.ToString());
        }
       
        int pageIndex = 1;
        int pageSize = 10;
        private void pagerToolsControl1_PagerIndexChanged(object sender, PagerToolsLibrary.C_EventArgsClass e)
        {
           /* MessageBox.Show("改变了当前页。" + Environment.NewLine
               + "当前页为：" + e.CurrentPagerIndex.ToString(), "提示"
               , MessageBoxButtons.OK, MessageBoxIcon.Asterisk);*/

            int pageIndex = e.CurrentPagerIndex;
            int pageSize = 10;

            int dc1 = 0;
            var result = Page(pageIndex, pageSize, out dc1);
            pagerToolsControl1.IsTrigger = false;
            pagerToolsControl1.DataCount = dc1; //result.Count; 当前查到数量
            pagerToolsControl1.Current = e.CurrentPagerIndex;
            pagerToolsControl1.IsTrigger = true;

            dataGridView1.DataSource = result;
            //   LoadData(pagerToolsControl1.PagerNum, pagerToolsControl1.Current);
        }

        private void pagerToolsControl1_PagerNumChanged(object sender, PagerToolsLibrary.C_EventArgsClass e)
        {
        /*  MessageBox.Show("改变了每页行数。" + Environment.NewLine
           + "当每页行数为：" + e.PagerNum.ToString(), "提示"
             , MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

            int iCount = aDbSession.From<TestTable>().Count();*/

            int pageIndex = e.CurrentPagerIndex;
            int pageSize = e.PagerNum;
            int dc1 = 0;
            var result = Page(pageIndex, pageSize, out dc1);
            pagerToolsControl1.IsTrigger = false;
            pagerToolsControl1.DataCount = dc1; //result.Count; 当前查到数量
            pagerToolsControl1.Current = e.CurrentPagerIndex;
            pagerToolsControl1.IsTrigger = true;
            dataGridView1.DataSource = result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var list = new List<Account>();
            for (int i = 10; i < 21; i++)
            {
                var model = new Account()
                {
                    Id = i.ToString(),
                    Name = "张三" + i.ToString(),
                    Password = "123456",
                    Email = "123@qq.com",
                    CreateTime = DateTime.Now,
                    Age = 15
                };
                list.Add(model);
            }
            using (var db = CreateDbBase())
            {
                var result = db.InsertBatch<Account>(list);
                if (result)
                    Console.WriteLine("添加成功");
                else
                    Console.WriteLine("添加失败");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
             pageIndex = 1;
             pageSize = 10;
            int dc1 = 0;
            var result= Page(pageIndex, pageSize,out dc1);
            pagerToolsControl1.IsTrigger = false;
            pagerToolsControl1.DataCount = dc1; //result.Count; 当前查到数量
            pagerToolsControl1.Current = 1;
            pagerToolsControl1.IsTrigger = true;

            dataGridView1.DataSource = result;
          
        }
        public IList<Account> Page(int pageIndex, int pageSize,out int dc1)//分布查询
        {
            using (var db = CreateDbBase())
            {

                var d = SqlQuery<Account>.Builder(db).AndWhere(m => m.Age, OperationMethod.Less, 20)
                     .LeftInclude()
                     .AndWhere(m => m.CreateTime, OperationMethod.Greater, DateTime.Now.AddDays(-5))
                     .AndWhere(m => m.Name, OperationMethod.Contains, "张")
                     .RightInclude()
                     .AndWhere(m => m.Age, OperationMethod.In, new List<int>() { 15 })
                     ;
                long dc = 0;
                var result = db.Page<Account>(pageIndex, pageSize, out dc, d);
                dc1 = (int)dc;
                // dataGridView1.DataSource = result;
                return result;
              //  Console.WriteLine("查询出数据条数:" + result.Count);
            }
        }
    }
}
